package com.tanhua.admin.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.SystemClock;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.tanhua.admin.mapper.AnalysisMapper;
import com.tanhua.admin.mapper.LogMapper;

import com.tanhua.model.Analysis;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.ParseException;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
public class AnalysisService {

    @Autowired
    private AnalysisMapper analysisMapper;

    @Autowired
    private LogMapper logMapper;

    /**
     * 定时统计日志数据到统计表中
     * 1、查询tb_log表中的数 （每日注册用户数，每日登陆用户，活跃的用户数据，次日留存的用户）
     * 2、构造AnalysisByDay对象
     * 3、完成统计数据的更新或者保存
     */
    public void analysis() throws ParseException, java.text.ParseException {
        //1、定义查询的日期
        String todayStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String yesdayStr = DateUtil.yesterday().toString("yyyy-MM-dd");
        //2、统计数据-注册数量
        Integer regCount = logMapper.queryByTypeAndLogTime("0101", todayStr);
        //3、统计数据-登录数量
        Integer loginCount = logMapper.queryByTypeAndLogTime("0101", todayStr);
        //4、统计数据-活跃数量
        Integer activeCount = logMapper.queryByLogTime(todayStr);
        //5、统计数据-次日留存
        Integer numRetention1d = logMapper.queryNumRetention1d(todayStr, yesdayStr);

        //6、根据日期查询数据
        QueryWrapper<Analysis> qw = new QueryWrapper<Analysis>();
        qw.eq("record_date", new SimpleDateFormat("yyyy-MM-dd").parse(todayStr));
        //7、构造Analysis对象
        Analysis analysis = analysisMapper.selectOne(qw);
        //8、如果存在，更新，如果不存在保存
        if (analysis != null) {
            analysis.setNumRegistered(regCount);
            analysis.setNumLogin(loginCount);
            analysis.setNumActive(activeCount);
            analysis.setNumRetention1d(numRetention1d);
            analysisMapper.updateById(analysis);
        } else {
            analysis = new Analysis();
            analysis.setNumRegistered(regCount);
            analysis.setNumLogin(loginCount);
            analysis.setNumActive(activeCount);
            analysis.setNumRetention1d(numRetention1d);
            analysis.setRecordDate(new SimpleDateFormat("yyyy-MM-dd").parse(todayStr));
            analysis.setCreated(new Date());
            analysisMapper.insert(analysis);
        }
    }

    //累计用户数
    public Integer queryCumulativeUsers() {
        return analysisMapper.queryCumulativeUsers();
    }

    //今日统计信息
    public Analysis querySummary(Date now) {
        //5、根据当前时间查询AnalysisByDay数据
        LambdaQueryWrapper<Analysis> qw = Wrappers.<Analysis>lambdaQuery();
        qw.eq(Analysis::getRecordDate, DateUtil.format(now, "yyyy-MM-dd"));
        return analysisMapper.selectOne(qw);
    }
    //查询新增
    public Map findDate(String start, String end,String laststart,String lastend) throws java.text.ParseException {
        //查询总数
        List<Map> thisYear = analysisMapper.selectNewSave(start,end);
        List<Map> lastYear = analysisMapper.selectNewSave(laststart,lastend);
        Map map = new HashMap<>();
        map.put("thisYear",thisYear);
        map.put("lastYear",lastYear);

        return map;
    }
    //查询活跃
    public Map findActive(String start, String end,String laststart,String lastend) {
        //查询总数
        List<Map> thisYear = analysisMapper.selectActive(start,end);
        List<Map> lastYear = analysisMapper.selectActive(laststart,lastend);
        Map map = new HashMap<>();
        map.put("thisYear",thisYear);
        map.put("lastYear",lastYear);
        return map;
    }
    //次日留存率
    public Map findRemain(String start, String end,String laststart,String lastend) {
        //查询总数
        List<Map> thisYear = analysisMapper.selectRemain(start,end);
        List<Map> lastYear = analysisMapper.selectRemain(laststart,lastend);
        Map map = new HashMap<>();
        map.put("thisYear",thisYear);
        map.put("lastYear",lastYear);
        return map;
    }

    public Integer querySummary7days() {
        return analysisMapper.queryNumActive7();
    }

    public Integer querySummary30days() {
        return analysisMapper.queryNumActive30();
    }
}